<?php


namespace App\Controller;

use App\Model\HyUser;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Snowflake\IdGeneratorInterface;
use Hyperf\Utils\Coroutine\Concurrent;

/**
 * Class SnowFlakeController
 * @package App\Controller
 * @AutoController(prefix="snowflake")
 */
class SnowFlakeController extends AbstractController
{
    /**
     * @Inject()
     * @var IdGeneratorInterface
     */
    protected $idGenerator;
    public function index()
    {
        // 生成秒级雪花ID
        $id = $this->idGenerator->generate();
        var_dump("生成的ID为：".$id."长度为：".strlen($id));
        $tranId = $this->idGenerator->degenerate($id);
        var_dump("转换回去后的值为：",$tranId);
        return [
            'msg'=>"生成成功"
        ];
    }

    // 创建一个用户
    public function test2()
    {
        $s = new Concurrent(200);
        for ($i = 0;$i<500000;$i++){
            echo "$i \n";
            $s->create(function () use($i){
                HyUser::query()->create(['name'=>"{$i}名称".uniqid()]);
            });
        }
        
        return [
            'mag'=>"创建成功"
        ];
    }
}